home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / RANDRECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  2.9 KB  |  102 lines

  1. /*------------------------------------------
  2.    RANDRECT.C -- Displays Random Rectangles
  3.                  (c) Charles Petzold, 1996
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10. void DrawRectangle (HWND) ;
  11.  
  12. int cxClient, cyClient ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16.      {
  17.      static char szAppName[] = "RandRect" ;
  18.      HWND        hwnd ;
  19.      MSG         msg ;
  20.      WNDCLASSEX  wndclass ;
  21.  
  22.      wndclass.cbSize        = sizeof (wndclass) ;
  23.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  24.      wndclass.lpfnWndProc   = WndProc ;
  25.      wndclass.cbClsExtra    = 0 ;
  26.      wndclass.cbWndExtra    = 0 ;
  27.      wndclass.hInstance     = hInstance ;
  28.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  29.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  30.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  31.      wndclass.lpszMenuName  = NULL ;
  32.      wndclass.lpszClassName = szAppName ;
  33.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  34.  
  35.      RegisterClassEx (&wndclass) ;
  36.  
  37.      hwnd = CreateWindow (szAppName, "Random Rectangles",
  38.                           WS_OVERLAPPEDWINDOW,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.  
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.  
  46.      while (TRUE)
  47.           {
  48.           if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  49.                {
  50.                if (msg.message == WM_QUIT)
  51.                     break ;
  52.  
  53.                TranslateMessage (&msg) ;
  54.                DispatchMessage (&msg) ;
  55.                }
  56.           else
  57.                DrawRectangle (hwnd) ;
  58.           }
  59.  
  60.      return msg.wParam ;
  61.      }
  62.  
  63. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  64.      {
  65.      switch (iMsg)
  66.           {
  67.           case WM_SIZE:
  68.                cxClient = LOWORD (lParam) ;
  69.                cyClient = HIWORD (lParam) ;
  70.                return 0 ;
  71.  
  72.           case WM_DESTROY:
  73.                PostQuitMessage (0) ;
  74.                return 0 ;
  75.           }
  76.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  77.      }
  78.  
  79. void DrawRectangle (HWND hwnd)
  80.      {
  81.      HBRUSH hBrush ;
  82.      HDC    hdc ;
  83.      RECT   rect ;
  84.  
  85.      if (cxClient == 0 || cyClient == 0)
  86.           return ;
  87.  
  88.      SetRect (&rect, rand () % cxClient, rand () % cyClient,
  89.                      rand () % cxClient, rand () % cyClient) ;
  90.  
  91.  
  92.      hBrush = CreateSolidBrush (RGB (rand () % 256, rand () % 256,
  93.                                                     rand () % 256)) ;
  94.      hdc = GetDC (hwnd) ;
  95.  
  96.      FillRect (hdc, &rect, hBrush) ;
  97.  
  98.      ReleaseDC (hwnd, hdc) ;
  99.  
  100.      DeleteObject (hBrush) ;
  101.      }     
  102.